//Copyright (C) 2025 Voltual
// 本程序是自由软件：你可以根据自由软件基金会发布的 GNU 通用公共许可证第3版
//（或任意更新的版本）的条款重新分发和/或修改它。
//本程序是基于希望它有用而分发的，但没有任何担保；甚至没有适销性或特定用途适用性的隐含担保。
// 有关更多细节，请参阅 GNU 通用公共许可证。
//
// 你应该已经收到了一份 GNU 通用公共许可证的副本
// 如果没有，请查阅 <http://www.gnu.org/licenses/>。
package cc.bbq.xq.ui.compose

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Comment
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import cc.bbq.xq.KtorClient
import cc.bbq.xq.ui.theme.messageCommentBg
import cc.bbq.xq.ui.theme.messageLikeBg
import cc.bbq.xq.ui.theme.messageDefaultBg // 新增导入
import cc.bbq.xq.ui.theme.billingIncome
import cc.bbq.xq.ui.theme.billingExpense

@Composable
fun ListItem(
    title: String,
    content: String,
    time: String,
    icon: ImageVector? = null,
    backgroundColor: Color = MaterialTheme.colorScheme.surface,
    contentColor: Color = MaterialTheme.colorScheme.onSurface,
    onClick: (() -> Unit)? = null
) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(8.dp)
            .then(if (onClick != null) Modifier.clickable(onClick = onClick) else Modifier),
        shape = RoundedCornerShape(8.dp),
        colors = CardDefaults.cardColors(
            containerColor = backgroundColor,
            contentColor = contentColor
        )
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                icon?.let {
                    Icon(
                        imageVector = it,
                        contentDescription = null,
                        tint = MaterialTheme.colorScheme.primary,
                        modifier = Modifier.size(24.dp)
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                }
                
                Text(
                    text = title,
                    style = MaterialTheme.typography.titleMedium,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier.weight(1f),
                    color = contentColor
                )
                
                Text(
                    text = time,
                    style = MaterialTheme.typography.labelSmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
            
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = content,
                style = MaterialTheme.typography.bodyMedium,
                color = contentColor
            )
        }
    }
}

@Composable
fun MessageItem(
    message: KtorClient.MessageNotification, // 修改为 KtorClient.MessageNotification
    onClick: () -> Unit
) {
    val (icon, bgColor) = when (message.type) {
        3 -> Icons.Default.Favorite to MaterialTheme.messageLikeBg
        5 -> Icons.AutoMirrored.Filled.Comment to MaterialTheme.messageCommentBg
        else -> Icons.Default.Notifications to MaterialTheme.messageDefaultBg
    }
    
    ListItem(
        title = message.title,
        content = message.content,
        time = message.time,
        icon = icon,
        backgroundColor = bgColor,
        onClick = onClick
    )
}

@Composable
fun BillingItem(billing: KtorClient.BillingItem) { // 修改为 KtorClient.BillingItem
    val amountColor = if (billing.transaction_amount.startsWith("+")) 
        MaterialTheme.billingIncome else MaterialTheme.billingExpense
    
    ListItem(
        title = "${getBillingTypeName(billing.transaction_type)}: ${billing.remark}",
        content = billing.transaction_amount,
        time = billing.transaction_date,
        contentColor = amountColor
    )
}

private fun getBillingTypeName(type: Int): String = when (type) {
    2 -> "签到奖励"
    6 -> "打赏"
    10 -> "评论打赏"
    else -> "交易"
}